

<!DOCTYPE html>
<!--[if IE 8]><html class="no-js lt-ie9" lang="en" > <![endif]-->
<!--[if gt IE 8]><!--> <html class="no-js" lang="en" > <!--<![endif]-->
<head>
  <meta charset="utf-8">
  
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  
  <title>quippy.vasp &mdash; quippy 7ea04c5+ documentation</title>
  

  
  
    <link rel="shortcut icon" href="../../_static/favicon.ico"/>
  
  
  

  
  <script type="text/javascript" src="../../_static/js/modernizr.min.js"></script>
  
    
      <script type="text/javascript" id="documentation_options" data-url_root="../../" src="../../_static/documentation_options.js"></script>
        <script type="text/javascript" src="../../_static/jquery.js"></script>
        <script type="text/javascript" src="../../_static/underscore.js"></script>
        <script type="text/javascript" src="../../_static/doctools.js"></script>
        <script type="text/javascript" src="../../_static/language_data.js"></script>
        <script async="async" type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.5/latest.js?config=TeX-AMS-MML_HTMLorMML"></script>
        <script type="text/x-mathjax-config">MathJax.Hub.Config({"tex2jax": {"processClass": "math|output_area", "processEscapes": true, "ignoreClass": "document", "inlineMath": [["$", "$"], ["\\(", "\\)"]]}})</script>
    
    <script type="text/javascript" src="../../_static/js/theme.js"></script>

    

  
  <link rel="stylesheet" href="../../_static/css/theme.css" type="text/css" />
  <link rel="stylesheet" href="../../_static/pygments.css" type="text/css" />
  <link rel="stylesheet" href="../../_static/theme_overrides.css" type="text/css" />
    <link rel="index" title="Index" href="../../genindex.html" />
    <link rel="search" title="Search" href="../../search.html" /> 
</head>

<body class="wy-body-for-nav">

   
  <div class="wy-grid-for-nav">
    
    <nav data-toggle="wy-nav-shift" class="wy-nav-side">
      <div class="wy-side-scroll">
        <div class="wy-side-nav-search" >
          

          
            <a href="../../index.html" class="icon icon-home"> quippy
          

          
            
            <img src="../../_static/hybrid.png" class="logo" alt="Logo"/>
          
          </a>

          
            
            
              <div class="version">
                7ea04c5+
              </div>
            
          

          
<div role="search">
  <form id="rtd-search-form" class="wy-form" action="../../search.html" method="get">
    <input type="text" name="q" placeholder="Search docs" />
    <input type="hidden" name="check_keywords" value="yes" />
    <input type="hidden" name="area" value="default" />
  </form>
</div>

          
        </div>

        <div class="wy-menu wy-menu-vertical" data-spy="affix" role="navigation" aria-label="main navigation">
          
    
            
            
              
            
            
              <ul>
<li class="toctree-l1"><a class="reference internal" href="../../intro.html">Introduction to <code class="docutils literal notranslate"><span class="pre">quippy</span></code></a></li>
<li class="toctree-l1"><a class="reference internal" href="../../install.html">Installation of QUIP and quippy</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../Tutorials/index.html">Tutorials</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../quippy.html">Quippy library reference</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../visualisation.html">Visualisation Tools</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../fortran_wrapper.html">Appendix: wrapping Fortran 90 Code</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../teach_sparse.html">Command line options of the teach_sparse main program</a></li>
</ul>

            
          
    <p></p>
    <ul>
        <li><a href="../../genindex.html">Index</a></li>
        <li><a href="../../py-modindex.html">Module Index</a></li>
        <li><a href="../../search.html">Search Page</a></li>
        <li><a href="../index.html">Module Source Code</a></li>
    </ul>
  
        </div>
      </div>
    </nav>

    <section data-toggle="wy-nav-shift" class="wy-nav-content-wrap">

      
      <nav class="wy-nav-top" aria-label="top navigation">
        
          <i data-toggle="wy-nav-top" class="fa fa-bars"></i>
          <a href="../../index.html">quippy</a>
        
      </nav>


      <div class="wy-nav-content">
        
        <div class="rst-content">
        
          















<div role="navigation" aria-label="breadcrumbs navigation">

  <ul class="wy-breadcrumbs">
    
      <li><a href="../../index.html">Docs</a> &raquo;</li>
        
          <li><a href="../index.html">Module code</a> &raquo;</li>
        
      <li>quippy.vasp</li>
    
    
      <li class="wy-breadcrumbs-aside">
        
      </li>
    
  </ul>

  
  <hr/>
</div>
          <div role="main" class="document" itemscope="itemscope" itemtype="http://schema.org/Article">
           <div itemprop="articleBody">
            
  <h1>Source code for quippy.vasp</h1><div class="highlight"><pre>
<span></span><span class="c1"># HQ XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX</span>
<span class="c1"># HQ X</span>
<span class="c1"># HQ X   quippy: Python interface to QUIP atomistic simulation library</span>
<span class="c1"># HQ X</span>
<span class="c1"># HQ X   Copyright James Kermode 2010</span>
<span class="c1"># HQ X</span>
<span class="c1"># HQ X   These portions of the source code are released under the GNU General</span>
<span class="c1"># HQ X   Public License, version 2, http://www.gnu.org/copyleft/gpl.html</span>
<span class="c1"># HQ X</span>
<span class="c1"># HQ X   If you would like to license the source code under different terms,</span>
<span class="c1"># HQ X   please contact James Kermode, james.kermode@gmail.com</span>
<span class="c1"># HQ X</span>
<span class="c1"># HQ X   When using this software, please cite the following reference:</span>
<span class="c1"># HQ X</span>
<span class="c1"># HQ X   http://www.jrkermode.co.uk/quippy</span>
<span class="c1"># HQ X</span>
<span class="c1"># HQ XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX</span>

<span class="kn">import</span> <span class="nn">quippy</span>
<span class="kn">from</span> <span class="nn">quippy.atoms</span> <span class="k">import</span> <span class="n">Atoms</span>
<span class="kn">from</span> <span class="nn">quippy.io</span> <span class="k">import</span> <span class="n">atoms_reader</span><span class="p">,</span> <span class="n">AtomsReaders</span><span class="p">,</span> <span class="n">AtomsWriters</span>
<span class="kn">from</span> <span class="nn">quippy.farray</span> <span class="k">import</span> <span class="n">fzeros</span><span class="p">,</span><span class="n">frange</span>
<span class="kn">import</span> <span class="nn">numpy</span> <span class="k">as</span> <span class="nn">np</span>
<span class="kn">import</span> <span class="nn">re</span><span class="o">,</span> <span class="nn">sys</span>

<span class="nd">@atoms_reader</span><span class="p">(</span><span class="s1">&#39;vasp&#39;</span><span class="p">)</span>
<span class="nd">@atoms_reader</span><span class="p">(</span><span class="s1">&#39;POSCAR&#39;</span><span class="p">)</span>
<span class="nd">@atoms_reader</span><span class="p">(</span><span class="s1">&#39;CONTCAR&#39;</span><span class="p">)</span>
<span class="k">def</span> <span class="nf">VASP_POSCAR_Reader</span><span class="p">(</span><span class="n">poscar</span><span class="p">,</span> <span class="n">species</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="nb">format</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;Read a configuration from a VASP POSCAR file.</span>

<span class="sd">    Following POSCAR, optionally also read a trajectory from an OUTCAR file.&quot;&quot;&quot;</span>

    <span class="n">p</span> <span class="o">=</span> <span class="nb">open</span><span class="p">(</span><span class="n">poscar</span><span class="p">,</span> <span class="s1">&#39;r&#39;</span><span class="p">)</span>
    <span class="n">comment</span> <span class="o">=</span> <span class="n">p</span><span class="o">.</span><span class="n">readline</span><span class="p">()</span><span class="o">.</span><span class="n">rstrip</span><span class="p">()</span>
    <span class="n">l</span> <span class="o">=</span> <span class="n">p</span><span class="o">.</span><span class="n">readline</span><span class="p">()</span><span class="o">.</span><span class="n">strip</span><span class="p">();</span> <span class="n">lc_factor</span><span class="o">=</span><span class="nb">float</span><span class="p">(</span><span class="n">l</span><span class="p">)</span>
    <span class="n">l</span> <span class="o">=</span> <span class="n">p</span><span class="o">.</span><span class="n">readline</span><span class="p">()</span><span class="o">.</span><span class="n">strip</span><span class="p">();</span> <span class="n">a1</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">real</span><span class="p">(</span><span class="n">l</span><span class="o">.</span><span class="n">split</span><span class="p">())</span>
    <span class="n">l</span> <span class="o">=</span> <span class="n">p</span><span class="o">.</span><span class="n">readline</span><span class="p">()</span><span class="o">.</span><span class="n">strip</span><span class="p">();</span> <span class="n">a2</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">real</span><span class="p">(</span><span class="n">l</span><span class="o">.</span><span class="n">split</span><span class="p">())</span>
    <span class="n">l</span> <span class="o">=</span> <span class="n">p</span><span class="o">.</span><span class="n">readline</span><span class="p">()</span><span class="o">.</span><span class="n">strip</span><span class="p">();</span> <span class="n">a3</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">real</span><span class="p">(</span><span class="n">l</span><span class="o">.</span><span class="n">split</span><span class="p">())</span>
    <span class="n">l</span> <span class="o">=</span> <span class="n">p</span><span class="o">.</span><span class="n">readline</span><span class="p">()</span><span class="o">.</span><span class="n">strip</span><span class="p">();</span> <span class="n">at_species</span> <span class="o">=</span> <span class="n">l</span><span class="o">.</span><span class="n">split</span><span class="p">()</span>
    <span class="k">try</span><span class="p">:</span>
        <span class="n">ns</span> <span class="o">=</span> <span class="p">[</span> <span class="nb">int</span><span class="p">(</span><span class="n">n</span><span class="p">)</span> <span class="k">for</span> <span class="n">n</span> <span class="ow">in</span> <span class="n">at_species</span> <span class="p">]</span>
        <span class="n">no_species</span> <span class="o">=</span> <span class="kc">True</span>
    <span class="k">except</span><span class="p">:</span>
        <span class="n">no_species</span> <span class="o">=</span> <span class="kc">False</span>

    <span class="n">have_species</span> <span class="o">=</span> <span class="kc">True</span>
    <span class="k">if</span> <span class="p">(</span><span class="n">no_species</span><span class="p">):</span>
        <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="nb">len</span><span class="p">(</span><span class="n">ns</span><span class="p">)):</span>
            <span class="k">if</span> <span class="p">(</span><span class="n">species</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">):</span>
                <span class="n">species_cli</span> <span class="o">=</span> <span class="n">species</span><span class="o">.</span><span class="n">split</span><span class="p">()</span>
                <span class="n">at_species</span><span class="p">[</span><span class="n">i</span><span class="p">]</span> <span class="o">=</span> <span class="n">species_cli</span><span class="p">[</span><span class="n">i</span><span class="o">-</span><span class="mi">1</span><span class="p">]</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="n">have_species</span> <span class="o">=</span> <span class="kc">False</span>
                <span class="n">at_species</span><span class="p">[</span><span class="n">i</span><span class="p">]</span> <span class="o">=</span> <span class="p">(</span><span class="s2">&quot;</span><span class="si">%d</span><span class="s2">&quot;</span> <span class="o">%</span> <span class="p">(</span><span class="n">i</span><span class="o">+</span><span class="mi">1</span><span class="p">))</span>
    <span class="k">else</span><span class="p">:</span>
        <span class="n">l</span> <span class="o">=</span> <span class="n">p</span><span class="o">.</span><span class="n">readline</span><span class="p">()</span><span class="o">.</span><span class="n">strip</span><span class="p">();</span>
        <span class="n">ns</span> <span class="o">=</span> <span class="p">[</span> <span class="nb">int</span><span class="p">(</span><span class="n">n</span><span class="p">)</span> <span class="k">for</span> <span class="n">n</span> <span class="ow">in</span> <span class="n">l</span><span class="o">.</span><span class="n">split</span><span class="p">()</span> <span class="p">]</span>

    <span class="n">l</span><span class="o">=</span><span class="n">p</span><span class="o">.</span><span class="n">readline</span><span class="p">()</span><span class="o">.</span><span class="n">strip</span><span class="p">()</span>
    <span class="k">if</span> <span class="p">(</span><span class="n">re</span><span class="o">.</span><span class="n">compile</span><span class="p">(</span><span class="s2">&quot;^\s*s&quot;</span><span class="p">,</span> <span class="n">re</span><span class="o">.</span><span class="n">IGNORECASE</span><span class="p">)</span><span class="o">.</span><span class="n">match</span><span class="p">(</span><span class="n">l</span><span class="p">)):</span>
        <span class="n">dyn_type</span> <span class="o">=</span> <span class="n">l</span>
        <span class="n">coord_type</span> <span class="o">=</span> <span class="n">p</span><span class="o">.</span><span class="n">readline</span><span class="p">()</span><span class="o">.</span><span class="n">strip</span><span class="p">();</span>
    <span class="k">else</span><span class="p">:</span>
        <span class="n">coord_type</span> <span class="o">=</span> <span class="n">l</span>

    <span class="n">n</span><span class="o">=</span><span class="mi">0</span>
    <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="nb">len</span><span class="p">(</span><span class="n">ns</span><span class="p">)):</span>
        <span class="n">n</span> <span class="o">+=</span> <span class="n">ns</span><span class="p">[</span><span class="n">i</span><span class="p">]</span>

    <span class="n">lat</span> <span class="o">=</span> <span class="n">fzeros</span><span class="p">(</span> <span class="p">(</span><span class="mi">3</span><span class="p">,</span><span class="mi">3</span><span class="p">)</span> <span class="p">)</span>
    <span class="n">lat</span><span class="p">[:,</span><span class="mi">1</span><span class="p">]</span> <span class="o">=</span> <span class="n">a1</span><span class="p">[</span><span class="mi">0</span><span class="p">:</span><span class="mi">3</span><span class="p">]</span>
    <span class="n">lat</span><span class="p">[:,</span><span class="mi">2</span><span class="p">]</span> <span class="o">=</span> <span class="n">a2</span><span class="p">[</span><span class="mi">0</span><span class="p">:</span><span class="mi">3</span><span class="p">]</span>
    <span class="n">lat</span><span class="p">[:,</span><span class="mi">3</span><span class="p">]</span> <span class="o">=</span> <span class="n">a3</span><span class="p">[</span><span class="mi">0</span><span class="p">:</span><span class="mi">3</span><span class="p">]</span>

    <span class="n">lat</span> <span class="o">*=</span> <span class="n">lc_factor</span>

    <span class="n">at</span> <span class="o">=</span> <span class="n">Atoms</span><span class="p">(</span><span class="n">n</span><span class="o">=</span><span class="n">n</span><span class="p">,</span> <span class="n">lattice</span><span class="o">=</span><span class="n">lat</span><span class="p">)</span>
    <span class="k">if</span> <span class="p">(</span><span class="nb">len</span><span class="p">(</span><span class="n">comment</span><span class="p">)</span> <span class="o">&gt;</span> <span class="mi">0</span><span class="p">):</span>
        <span class="n">at</span><span class="o">.</span><span class="n">params</span><span class="p">[</span><span class="s1">&#39;VASP_Comment&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="n">comment</span>

    <span class="n">coord_direct</span><span class="o">=</span><span class="n">re</span><span class="o">.</span><span class="n">compile</span><span class="p">(</span><span class="s2">&quot;^\s*d&quot;</span><span class="p">,</span> <span class="n">re</span><span class="o">.</span><span class="n">IGNORECASE</span><span class="p">)</span><span class="o">.</span><span class="n">match</span><span class="p">(</span><span class="n">coord_type</span><span class="p">);</span>

    <span class="n">ii</span> <span class="o">=</span> <span class="mi">1</span>
    <span class="k">for</span> <span class="n">ti</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="nb">len</span><span class="p">(</span><span class="n">ns</span><span class="p">)):</span>
        <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="n">ns</span><span class="p">[</span><span class="n">ti</span><span class="p">]):</span>
            <span class="n">l</span> <span class="o">=</span> <span class="n">p</span><span class="o">.</span><span class="n">readline</span><span class="p">()</span><span class="o">.</span><span class="n">strip</span><span class="p">();</span> <span class="n">pos</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">(</span><span class="n">l</span><span class="o">.</span><span class="n">split</span><span class="p">()[</span><span class="mi">0</span><span class="p">:</span><span class="mi">3</span><span class="p">],</span> <span class="nb">float</span><span class="p">);</span>
            <span class="k">if</span> <span class="p">(</span><span class="n">coord_direct</span><span class="p">):</span>
                <span class="n">at</span><span class="o">.</span><span class="n">pos</span><span class="p">[:,</span><span class="n">ii</span><span class="p">]</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">dot</span><span class="p">(</span><span class="n">at</span><span class="o">.</span><span class="n">lattice</span><span class="p">[:,:],</span><span class="n">pos</span><span class="p">[:])</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="n">at</span><span class="o">.</span><span class="n">pos</span><span class="p">[:,</span><span class="n">ii</span><span class="p">]</span> <span class="o">=</span> <span class="n">pos</span><span class="p">[:]</span><span class="o">*</span><span class="n">lc_factor</span>
            <span class="n">at</span><span class="o">.</span><span class="n">species</span><span class="p">[:,</span><span class="n">ii</span><span class="p">]</span> <span class="o">=</span> <span class="n">at_species</span><span class="p">[</span><span class="n">ti</span><span class="p">]</span>
            <span class="n">ii</span> <span class="o">+=</span> <span class="mi">1</span>

    <span class="k">if</span> <span class="p">(</span><span class="n">have_species</span><span class="p">):</span>
        <span class="n">at</span><span class="o">.</span><span class="n">set_zs</span><span class="p">()</span>
    <span class="k">else</span><span class="p">:</span>
        <span class="n">at</span><span class="o">.</span><span class="n">Z</span><span class="p">[:]</span> <span class="o">=</span> <span class="p">[</span> <span class="nb">int</span><span class="p">(</span><span class="s2">&quot;&quot;</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="n">n</span><span class="p">))</span> <span class="k">for</span> <span class="n">n</span> <span class="ow">in</span> <span class="n">at</span><span class="o">.</span><span class="n">species</span><span class="p">[:]</span> <span class="p">]</span>

    <span class="k">yield</span> <span class="n">at</span>

<span class="nd">@atoms_reader</span><span class="p">(</span><span class="s1">&#39;XDATCAR&#39;</span><span class="p">)</span>
<span class="k">def</span> <span class="nf">VASP_XDATCAR_Reader</span><span class="p">(</span><span class="n">xdatcar</span><span class="p">,</span> <span class="n">species</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="nb">format</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>

    <span class="n">p</span> <span class="o">=</span> <span class="nb">open</span><span class="p">(</span><span class="n">xdatcar</span><span class="p">,</span> <span class="s1">&#39;r&#39;</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">read_header</span><span class="p">(</span><span class="n">xdatcar</span><span class="p">):</span>
       <span class="n">comment</span> <span class="o">=</span> <span class="n">p</span><span class="o">.</span><span class="n">readline</span><span class="p">()</span><span class="o">.</span><span class="n">rstrip</span><span class="p">()</span>
       <span class="k">if</span> <span class="n">comment</span><span class="o">.</span><span class="n">find</span><span class="p">(</span><span class="s2">&quot;Direct configuration&quot;</span><span class="p">)</span> <span class="o">==</span> <span class="mi">0</span><span class="p">:</span> <span class="c1"># is the beginning of some positions</span>
	 <span class="k">return</span> <span class="p">(</span><span class="kc">None</span><span class="p">,</span> <span class="kc">None</span><span class="p">,</span> <span class="n">comment</span><span class="p">,</span> <span class="kc">None</span><span class="p">,</span> <span class="kc">None</span><span class="p">,</span> <span class="kc">None</span><span class="p">)</span>
       <span class="n">l</span> <span class="o">=</span> <span class="n">p</span><span class="o">.</span><span class="n">readline</span><span class="p">()</span><span class="o">.</span><span class="n">strip</span><span class="p">();</span> <span class="n">lc_factor</span><span class="o">=</span><span class="nb">float</span><span class="p">(</span><span class="n">l</span><span class="p">)</span>
       <span class="n">l</span> <span class="o">=</span> <span class="n">p</span><span class="o">.</span><span class="n">readline</span><span class="p">()</span><span class="o">.</span><span class="n">strip</span><span class="p">();</span> <span class="n">a1</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">real</span><span class="p">(</span><span class="n">l</span><span class="o">.</span><span class="n">split</span><span class="p">())</span>
       <span class="n">l</span> <span class="o">=</span> <span class="n">p</span><span class="o">.</span><span class="n">readline</span><span class="p">()</span><span class="o">.</span><span class="n">strip</span><span class="p">();</span> <span class="n">a2</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">real</span><span class="p">(</span><span class="n">l</span><span class="o">.</span><span class="n">split</span><span class="p">())</span>
       <span class="n">l</span> <span class="o">=</span> <span class="n">p</span><span class="o">.</span><span class="n">readline</span><span class="p">()</span><span class="o">.</span><span class="n">strip</span><span class="p">();</span> <span class="n">a3</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">real</span><span class="p">(</span><span class="n">l</span><span class="o">.</span><span class="n">split</span><span class="p">())</span>
       <span class="n">l</span> <span class="o">=</span> <span class="n">p</span><span class="o">.</span><span class="n">readline</span><span class="p">()</span><span class="o">.</span><span class="n">strip</span><span class="p">();</span> <span class="n">at_species</span> <span class="o">=</span> <span class="n">l</span><span class="o">.</span><span class="n">split</span><span class="p">()</span>
       <span class="k">try</span><span class="p">:</span>
	   <span class="n">ns</span> <span class="o">=</span> <span class="p">[</span> <span class="nb">int</span><span class="p">(</span><span class="n">n</span><span class="p">)</span> <span class="k">for</span> <span class="n">n</span> <span class="ow">in</span> <span class="n">at_species</span> <span class="p">]</span>
	   <span class="n">no_species_read</span> <span class="o">=</span> <span class="kc">True</span>
       <span class="k">except</span><span class="p">:</span>
	   <span class="n">no_species_read</span> <span class="o">=</span> <span class="kc">False</span>

       <span class="k">if</span> <span class="n">species</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
	  <span class="n">at_species</span> <span class="o">=</span> <span class="n">species</span><span class="o">.</span><span class="n">split</span><span class="p">()</span>
	  <span class="n">have_species</span> <span class="o">=</span> <span class="kc">True</span>
       <span class="k">else</span><span class="p">:</span>
	  <span class="k">if</span> <span class="n">no_species_read</span><span class="p">:</span>
	     <span class="n">have_species</span> <span class="o">=</span> <span class="kc">False</span>
	     <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="nb">len</span><span class="p">(</span><span class="n">ns</span><span class="p">)):</span>
		<span class="n">at_species</span><span class="p">[</span><span class="n">i</span><span class="p">]</span> <span class="o">=</span> <span class="p">(</span><span class="s2">&quot;</span><span class="si">%d</span><span class="s2">&quot;</span> <span class="o">%</span> <span class="p">(</span><span class="n">i</span><span class="o">+</span><span class="mi">1</span><span class="p">))</span>
	  <span class="k">else</span><span class="p">:</span>
	    <span class="n">have_species</span> <span class="o">=</span> <span class="kc">True</span>

       <span class="k">if</span> <span class="ow">not</span> <span class="n">no_species_read</span><span class="p">:</span>
          <span class="n">l</span> <span class="o">=</span> <span class="n">p</span><span class="o">.</span><span class="n">readline</span><span class="p">()</span><span class="o">.</span><span class="n">strip</span><span class="p">();</span>
          <span class="n">ns</span> <span class="o">=</span> <span class="p">[</span> <span class="nb">int</span><span class="p">(</span><span class="n">n</span><span class="p">)</span> <span class="k">for</span> <span class="n">n</span> <span class="ow">in</span> <span class="n">l</span><span class="o">.</span><span class="n">split</span><span class="p">()</span> <span class="p">]</span>

       <span class="n">n</span><span class="o">=</span><span class="mi">0</span>
       <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="nb">len</span><span class="p">(</span><span class="n">ns</span><span class="p">)):</span>
	   <span class="n">n</span> <span class="o">+=</span> <span class="n">ns</span><span class="p">[</span><span class="n">i</span><span class="p">]</span>

       <span class="n">lat</span> <span class="o">=</span> <span class="n">fzeros</span><span class="p">(</span> <span class="p">(</span><span class="mi">3</span><span class="p">,</span><span class="mi">3</span><span class="p">)</span> <span class="p">)</span>
       <span class="n">lat</span><span class="p">[:,</span><span class="mi">1</span><span class="p">]</span> <span class="o">=</span> <span class="n">a1</span><span class="p">[</span><span class="mi">0</span><span class="p">:</span><span class="mi">3</span><span class="p">]</span>
       <span class="n">lat</span><span class="p">[:,</span><span class="mi">2</span><span class="p">]</span> <span class="o">=</span> <span class="n">a2</span><span class="p">[</span><span class="mi">0</span><span class="p">:</span><span class="mi">3</span><span class="p">]</span>
       <span class="n">lat</span><span class="p">[:,</span><span class="mi">3</span><span class="p">]</span> <span class="o">=</span> <span class="n">a3</span><span class="p">[</span><span class="mi">0</span><span class="p">:</span><span class="mi">3</span><span class="p">]</span>

       <span class="n">lat</span> <span class="o">*=</span> <span class="n">lc_factor</span>
       <span class="k">return</span> <span class="p">(</span><span class="n">lat</span><span class="p">,</span> <span class="n">n</span><span class="p">,</span> <span class="n">comment</span><span class="p">,</span> <span class="n">ns</span><span class="p">,</span> <span class="n">at_species</span><span class="p">,</span> <span class="n">have_species</span><span class="p">)</span>

    <span class="p">(</span><span class="n">lat</span><span class="p">,</span> <span class="n">n</span><span class="p">,</span> <span class="n">comment</span><span class="p">,</span> <span class="n">ns</span><span class="p">,</span> <span class="n">at_species</span><span class="p">,</span> <span class="n">have_species</span><span class="p">)</span> <span class="o">=</span> <span class="n">read_header</span><span class="p">(</span><span class="n">xdatcar</span><span class="p">)</span>
    <span class="c1"># end of header</span>

    <span class="k">while</span> <span class="kc">True</span><span class="p">:</span>
       <span class="k">try</span><span class="p">:</span>
	  <span class="p">(</span><span class="n">new_lat</span><span class="p">,</span> <span class="n">new_n</span><span class="p">,</span> <span class="n">new_comment</span><span class="p">,</span> <span class="n">new_ns</span><span class="p">,</span> <span class="n">new_at_species</span><span class="p">,</span> <span class="n">new_have_species</span><span class="p">)</span> <span class="o">=</span> <span class="n">read_header</span><span class="p">(</span><span class="n">xdatcar</span><span class="p">)</span>
       <span class="k">except</span><span class="p">:</span>
	  <span class="k">return</span>

       <span class="k">if</span> <span class="n">new_lat</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span> <span class="c1"># wasn&#39;t another header</span>
	  <span class="n">l</span> <span class="o">=</span> <span class="n">new_comment</span>
       <span class="k">else</span><span class="p">:</span>
	  <span class="n">lat</span> <span class="o">=</span> <span class="n">new_lat</span> <span class="c1"># assume nothing else has changed</span>
	  <span class="n">l</span><span class="o">=</span><span class="n">p</span><span class="o">.</span><span class="n">readline</span><span class="p">()</span><span class="o">.</span><span class="n">strip</span><span class="p">()</span>

       <span class="k">if</span> <span class="p">(</span><span class="n">re</span><span class="o">.</span><span class="n">compile</span><span class="p">(</span><span class="s2">&quot;^\s*s&quot;</span><span class="p">,</span> <span class="n">re</span><span class="o">.</span><span class="n">IGNORECASE</span><span class="p">)</span><span class="o">.</span><span class="n">match</span><span class="p">(</span><span class="n">l</span><span class="p">)):</span>
	   <span class="n">dyn_type</span> <span class="o">=</span> <span class="n">l</span>
	   <span class="n">coord_type</span> <span class="o">=</span> <span class="n">p</span><span class="o">.</span><span class="n">readline</span><span class="p">()</span><span class="o">.</span><span class="n">strip</span><span class="p">();</span>
       <span class="k">else</span><span class="p">:</span>
	   <span class="n">coord_type</span> <span class="o">=</span> <span class="n">l</span>

       <span class="n">at</span> <span class="o">=</span> <span class="n">Atoms</span><span class="p">(</span><span class="n">n</span><span class="o">=</span><span class="n">n</span><span class="p">,</span> <span class="n">lattice</span><span class="o">=</span><span class="n">lat</span><span class="p">)</span>
       <span class="k">if</span> <span class="p">(</span><span class="nb">len</span><span class="p">(</span><span class="n">comment</span><span class="p">)</span> <span class="o">&gt;</span> <span class="mi">0</span><span class="p">):</span>
	   <span class="n">at</span><span class="o">.</span><span class="n">params</span><span class="p">[</span><span class="s1">&#39;VASP_Comment&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="n">comment</span>

       <span class="n">coord_direct</span><span class="o">=</span><span class="n">re</span><span class="o">.</span><span class="n">compile</span><span class="p">(</span><span class="s2">&quot;^\s*d&quot;</span><span class="p">,</span> <span class="n">re</span><span class="o">.</span><span class="n">IGNORECASE</span><span class="p">)</span><span class="o">.</span><span class="n">match</span><span class="p">(</span><span class="n">coord_type</span><span class="p">);</span>

       <span class="n">ii</span> <span class="o">=</span> <span class="mi">1</span>
       <span class="k">for</span> <span class="n">ti</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="nb">len</span><span class="p">(</span><span class="n">ns</span><span class="p">)):</span>
	   <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="n">ns</span><span class="p">[</span><span class="n">ti</span><span class="p">]):</span>
	       <span class="n">l</span> <span class="o">=</span> <span class="n">p</span><span class="o">.</span><span class="n">readline</span><span class="p">()</span><span class="o">.</span><span class="n">strip</span><span class="p">();</span> <span class="n">pos</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">(</span><span class="n">l</span><span class="o">.</span><span class="n">split</span><span class="p">()[</span><span class="mi">0</span><span class="p">:</span><span class="mi">3</span><span class="p">],</span> <span class="nb">float</span><span class="p">);</span>
	       <span class="k">if</span> <span class="p">(</span><span class="n">coord_direct</span><span class="p">):</span>
		   <span class="n">at</span><span class="o">.</span><span class="n">pos</span><span class="p">[:,</span><span class="n">ii</span><span class="p">]</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">dot</span><span class="p">(</span><span class="n">at</span><span class="o">.</span><span class="n">lattice</span><span class="p">[:,:],</span><span class="n">pos</span><span class="p">[:])</span>
	       <span class="k">else</span><span class="p">:</span>
		   <span class="n">at</span><span class="o">.</span><span class="n">pos</span><span class="p">[:,</span><span class="n">ii</span><span class="p">]</span> <span class="o">=</span> <span class="n">pos</span><span class="p">[:]</span><span class="o">*</span><span class="n">lc_factor</span>
	       <span class="n">at</span><span class="o">.</span><span class="n">species</span><span class="p">[:,</span><span class="n">ii</span><span class="p">]</span> <span class="o">=</span> <span class="n">at_species</span><span class="p">[</span><span class="n">ti</span><span class="p">]</span>
	       <span class="n">ii</span> <span class="o">+=</span> <span class="mi">1</span>

       <span class="k">if</span> <span class="p">(</span><span class="n">have_species</span><span class="p">):</span>
	   <span class="n">at</span><span class="o">.</span><span class="n">set_zs</span><span class="p">()</span>
       <span class="k">else</span><span class="p">:</span>
	   <span class="n">at</span><span class="o">.</span><span class="n">Z</span><span class="p">[:]</span> <span class="o">=</span> <span class="p">[</span> <span class="nb">int</span><span class="p">(</span><span class="s2">&quot;&quot;</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="n">n</span><span class="p">))</span> <span class="k">for</span> <span class="n">n</span> <span class="ow">in</span> <span class="n">at</span><span class="o">.</span><span class="n">species</span><span class="p">[:]</span> <span class="p">]</span>

       <span class="k">yield</span> <span class="n">at</span>


<div class="viewcode-block" id="VASPWriter"><a class="viewcode-back" href="../../io.html#quippy.vasp.VASPWriter">[docs]</a><span class="k">class</span> <span class="nc">VASPWriter</span><span class="p">(</span><span class="nb">object</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Writer for VASP POSCAR format</span>
<span class="sd">    &quot;&quot;&quot;</span>

    <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">out</span><span class="p">,</span> <span class="n">species_list</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">out</span><span class="o">=</span><span class="n">out</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">opened</span> <span class="o">=</span> <span class="kc">False</span>
        <span class="k">if</span> <span class="nb">type</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">out</span><span class="p">)</span> <span class="o">==</span> <span class="nb">type</span><span class="p">(</span><span class="s1">&#39;&#39;</span><span class="p">):</span>
            <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">out</span> <span class="o">==</span> <span class="s1">&#39;stdout&#39;</span><span class="p">:</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">out</span> <span class="o">=</span> <span class="n">sys</span><span class="o">.</span><span class="n">stdout</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">out</span> <span class="o">=</span> <span class="nb">open</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">out</span><span class="p">,</span> <span class="s1">&#39;w&#39;</span><span class="p">)</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">opened</span> <span class="o">=</span> <span class="kc">True</span>
        <span class="k">if</span> <span class="n">species_list</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">species_list</span> <span class="o">=</span> <span class="n">species_list</span>

    <span class="k">def</span> <span class="nf">close</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">opened</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">out</span><span class="o">.</span><span class="n">close</span><span class="p">()</span>

    <span class="k">def</span> <span class="nf">write</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">at</span><span class="p">):</span>

        <span class="c1"># find numbers of atoms of each type, property name and value to match, and property labels to print above numbers</span>
        <span class="n">atnums</span> <span class="o">=</span> <span class="p">[]</span>
        <span class="n">prop_vals</span> <span class="o">=</span> <span class="p">[]</span>
        <span class="n">prop_vals_map</span> <span class="o">=</span> <span class="p">{}</span>
        <span class="n">labels</span> <span class="o">=</span> <span class="p">[]</span>
        <span class="k">if</span> <span class="p">(</span><span class="nb">hasattr</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="s1">&#39;species_list&#39;</span><span class="p">)):</span>
            <span class="k">if</span> <span class="p">(</span><span class="ow">not</span> <span class="nb">hasattr</span><span class="p">(</span><span class="n">at</span><span class="p">,</span> <span class="s1">&#39;species&#39;</span><span class="p">)):</span>
                <span class="n">sys</span><span class="o">.</span><span class="n">stderr</span><span class="o">.</span><span class="n">write</span><span class="p">(</span><span class="s2">&quot;VASP writer needs species property when species_list is specified&quot;</span><span class="p">)</span>
                <span class="n">sys</span><span class="o">.</span><span class="n">exit</span><span class="p">(</span><span class="mi">1</span><span class="p">)</span>
            <span class="nb">property</span><span class="o">=</span><span class="s1">&#39;species&#39;</span>
            <span class="k">for</span> <span class="n">s</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">species_list</span><span class="p">:</span>
                <span class="n">labels</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">s</span><span class="p">)</span>
                <span class="n">prop_vals</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">s</span><span class="p">)</span>
                <span class="n">prop_vals_map</span><span class="p">[</span><span class="n">s</span><span class="p">]</span> <span class="o">=</span> <span class="mi">1</span>
                <span class="n">atnums</span><span class="o">.</span><span class="n">append</span><span class="p">((</span><span class="n">at</span><span class="o">.</span><span class="n">species</span><span class="p">[:]</span><span class="o">.</span><span class="n">stripstrings</span><span class="p">()</span> <span class="o">==</span> <span class="n">s</span><span class="p">)</span><span class="o">.</span><span class="n">count</span><span class="p">())</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="nb">property</span><span class="o">=</span><span class="s1">&#39;Z&#39;</span>
            <span class="n">atnums_map</span> <span class="o">=</span> <span class="p">{}</span>
            <span class="k">for</span> <span class="n">i_at</span> <span class="ow">in</span> <span class="n">frange</span><span class="p">(</span><span class="n">at</span><span class="o">.</span><span class="n">n</span><span class="p">):</span>
                <span class="k">try</span><span class="p">:</span>
                    <span class="n">atnums_map</span><span class="p">[</span><span class="s2">&quot;</span><span class="si">%d</span><span class="s2">&quot;</span> <span class="o">%</span> <span class="n">at</span><span class="o">.</span><span class="n">Z</span><span class="p">[</span><span class="n">i_at</span><span class="p">]]</span> <span class="o">+=</span> <span class="mi">1</span>
                <span class="k">except</span><span class="p">:</span>
                    <span class="n">atnums_map</span><span class="p">[</span><span class="s2">&quot;</span><span class="si">%d</span><span class="s2">&quot;</span> <span class="o">%</span> <span class="n">at</span><span class="o">.</span><span class="n">Z</span><span class="p">[</span><span class="n">i_at</span><span class="p">]]</span> <span class="o">=</span> <span class="mi">1</span>

        <span class="k">for</span> <span class="n">Z_s</span> <span class="ow">in</span> <span class="nb">sorted</span><span class="p">(</span><span class="n">atnums_map</span><span class="o">.</span><span class="n">keys</span><span class="p">(),</span> <span class="n">key</span> <span class="o">=</span> <span class="k">lambda</span> <span class="n">entry</span><span class="p">:</span> <span class="nb">int</span><span class="p">(</span><span class="n">entry</span><span class="p">)):</span>
            <span class="n">prop_vals</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="nb">int</span><span class="p">(</span><span class="n">Z_s</span><span class="p">))</span>
            <span class="n">prop_vals_map</span><span class="p">[</span><span class="nb">int</span><span class="p">(</span><span class="n">Z_s</span><span class="p">)]</span> <span class="o">=</span> <span class="mi">1</span>
            <span class="n">labels</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">quippy</span><span class="o">.</span><span class="n">ElementName</span><span class="p">[</span><span class="nb">int</span><span class="p">(</span><span class="n">Z_s</span><span class="p">)])</span>
            <span class="n">atnums</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="nb">int</span><span class="p">(</span><span class="n">atnums_map</span><span class="p">[</span><span class="n">Z_s</span><span class="p">]))</span>

        <span class="c1"># check that every atom has a valid type</span>
        <span class="k">for</span> <span class="n">i_at</span> <span class="ow">in</span> <span class="n">frange</span><span class="p">(</span><span class="n">at</span><span class="o">.</span><span class="n">n</span><span class="p">):</span>
            <span class="k">try</span><span class="p">:</span>
                <span class="n">prop</span> <span class="o">=</span> <span class="nb">getattr</span><span class="p">(</span><span class="n">at</span><span class="p">,</span><span class="nb">property</span><span class="p">)[</span><span class="n">i_at</span><span class="p">]</span><span class="o">.</span><span class="n">stripstrings</span><span class="p">()</span>
            <span class="k">except</span><span class="p">:</span>
                <span class="n">prop</span> <span class="o">=</span> <span class="nb">getattr</span><span class="p">(</span><span class="n">at</span><span class="p">,</span><span class="nb">property</span><span class="p">)[</span><span class="n">i_at</span><span class="p">]</span>

            <span class="k">if</span> <span class="ow">not</span> <span class="n">prop</span> <span class="ow">in</span> <span class="n">prop_vals_map</span><span class="p">:</span>
                <span class="c1"># should probably handle situation when prop isn&#39;t a string, but that should never happen</span>
                <span class="n">sys</span><span class="o">.</span><span class="n">stderr</span><span class="o">.</span><span class="n">write</span><span class="p">(</span><span class="s2">&quot;Failed to find property </span><span class="si">%s</span><span class="s2"> in prop_vals_map dictionary&quot;</span> <span class="o">%</span> <span class="n">prop</span><span class="p">)</span>
                <span class="n">sys</span><span class="o">.</span><span class="n">exit</span><span class="p">(</span><span class="mi">1</span><span class="p">)</span>

        <span class="n">swapped_a1_a2</span> <span class="o">=</span> <span class="kc">False</span>
        <span class="n">vol</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">dot</span><span class="p">(</span><span class="n">at</span><span class="o">.</span><span class="n">lattice</span><span class="p">[:,</span><span class="mi">1</span><span class="p">],</span><span class="n">np</span><span class="o">.</span><span class="n">cross</span><span class="p">(</span><span class="n">at</span><span class="o">.</span><span class="n">lattice</span><span class="p">[:,</span><span class="mi">2</span><span class="p">],</span><span class="n">at</span><span class="o">.</span><span class="n">lattice</span><span class="p">[:,</span><span class="mi">3</span><span class="p">]))</span>
        <span class="k">if</span> <span class="p">(</span><span class="n">vol</span> <span class="o">&lt;</span> <span class="mf">0.0</span><span class="p">):</span>
            <span class="n">t_a1</span> <span class="o">=</span> <span class="n">at</span><span class="o">.</span><span class="n">lattice</span><span class="p">[:,</span><span class="mi">1</span><span class="p">]</span><span class="o">.</span><span class="n">copy</span><span class="p">()</span>
            <span class="n">at</span><span class="o">.</span><span class="n">lattice</span><span class="p">[:,</span><span class="mi">1</span><span class="p">]</span> <span class="o">=</span> <span class="n">at</span><span class="o">.</span><span class="n">lattice</span><span class="p">[:,</span><span class="mi">2</span><span class="p">]</span>
            <span class="n">at</span><span class="o">.</span><span class="n">lattice</span><span class="p">[:,</span><span class="mi">2</span><span class="p">]</span> <span class="o">=</span> <span class="n">t_a1</span><span class="p">[:]</span>
            <span class="n">swapped_a1_a2</span> <span class="o">=</span> <span class="kc">True</span>
            <span class="n">sys</span><span class="o">.</span><span class="n">stderr</span><span class="o">.</span><span class="n">write</span><span class="p">(</span><span class="s2">&quot;WARNING: swapped a1 and a2 to get positive scalar triple product</span><span class="se">\n</span><span class="s2">&quot;</span><span class="p">)</span>

        <span class="c1"># Comment</span>
        <span class="k">try</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">out</span><span class="o">.</span><span class="n">write</span><span class="p">(</span><span class="n">at</span><span class="o">.</span><span class="n">params</span><span class="p">[</span><span class="s1">&#39;VASP_Comment&#39;</span><span class="p">])</span>
            <span class="k">if</span> <span class="p">(</span><span class="n">swapped_a1_a2</span><span class="p">):</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">out</span><span class="o">.</span><span class="n">write</span><span class="p">(</span><span class="s2">&quot; a1 and a2 swapped relative to input to get positive volume&quot;</span><span class="p">)</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">out</span><span class="o">.</span><span class="n">write</span><span class="p">(</span><span class="s2">&quot;</span><span class="se">\n</span><span class="s2">&quot;</span><span class="p">)</span>
        <span class="k">except</span><span class="p">:</span>
            <span class="k">try</span><span class="p">:</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">out</span><span class="o">.</span><span class="n">write</span><span class="p">(</span><span class="n">at</span><span class="o">.</span><span class="n">params</span><span class="p">[</span><span class="s1">&#39;comment&#39;</span><span class="p">])</span>
            <span class="k">except</span><span class="p">:</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">out</span><span class="o">.</span><span class="n">write</span><span class="p">(</span><span class="s1">&#39;&#39;</span><span class="p">)</span>
            <span class="k">if</span> <span class="p">(</span><span class="n">swapped_a1_a2</span><span class="p">):</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">out</span><span class="o">.</span><span class="n">write</span><span class="p">(</span><span class="s2">&quot; a1 and a2 swapped relative to input to get positive volume&quot;</span><span class="p">)</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">out</span><span class="o">.</span><span class="n">write</span><span class="p">(</span><span class="s2">&quot;</span><span class="se">\n</span><span class="s2">&quot;</span><span class="p">)</span>

        <span class="c1"># Lattice</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">out</span><span class="o">.</span><span class="n">write</span><span class="p">(</span><span class="s2">&quot;1.0</span><span class="se">\n</span><span class="s2">&quot;</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">out</span><span class="o">.</span><span class="n">write</span><span class="p">(</span><span class="s2">&quot;</span><span class="si">%.12f</span><span class="s2"> </span><span class="si">%.12f</span><span class="s2"> </span><span class="si">%.12f</span><span class="se">\n</span><span class="s2">&quot;</span> <span class="o">%</span> <span class="p">(</span><span class="n">at</span><span class="o">.</span><span class="n">lattice</span><span class="p">[</span><span class="mi">1</span><span class="p">,</span><span class="mi">1</span><span class="p">],</span> <span class="n">at</span><span class="o">.</span><span class="n">lattice</span><span class="p">[</span><span class="mi">2</span><span class="p">,</span><span class="mi">1</span><span class="p">],</span> <span class="n">at</span><span class="o">.</span><span class="n">lattice</span><span class="p">[</span><span class="mi">3</span><span class="p">,</span><span class="mi">1</span><span class="p">]))</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">out</span><span class="o">.</span><span class="n">write</span><span class="p">(</span><span class="s2">&quot;</span><span class="si">%.12f</span><span class="s2"> </span><span class="si">%.12f</span><span class="s2"> </span><span class="si">%.12f</span><span class="se">\n</span><span class="s2">&quot;</span> <span class="o">%</span> <span class="p">(</span><span class="n">at</span><span class="o">.</span><span class="n">lattice</span><span class="p">[</span><span class="mi">1</span><span class="p">,</span><span class="mi">2</span><span class="p">],</span> <span class="n">at</span><span class="o">.</span><span class="n">lattice</span><span class="p">[</span><span class="mi">2</span><span class="p">,</span><span class="mi">2</span><span class="p">],</span> <span class="n">at</span><span class="o">.</span><span class="n">lattice</span><span class="p">[</span><span class="mi">3</span><span class="p">,</span><span class="mi">2</span><span class="p">]))</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">out</span><span class="o">.</span><span class="n">write</span><span class="p">(</span><span class="s2">&quot;</span><span class="si">%.12f</span><span class="s2"> </span><span class="si">%.12f</span><span class="s2"> </span><span class="si">%.12f</span><span class="se">\n</span><span class="s2">&quot;</span> <span class="o">%</span> <span class="p">(</span><span class="n">at</span><span class="o">.</span><span class="n">lattice</span><span class="p">[</span><span class="mi">1</span><span class="p">,</span><span class="mi">3</span><span class="p">],</span> <span class="n">at</span><span class="o">.</span><span class="n">lattice</span><span class="p">[</span><span class="mi">2</span><span class="p">,</span><span class="mi">3</span><span class="p">],</span> <span class="n">at</span><span class="o">.</span><span class="n">lattice</span><span class="p">[</span><span class="mi">3</span><span class="p">,</span><span class="mi">3</span><span class="p">]))</span>

        <span class="c1"># Numbers of atoms and type labels</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">out</span><span class="o">.</span><span class="n">write</span><span class="p">(</span><span class="s2">&quot; &quot;</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="n">labels</span><span class="p">)</span><span class="o">+</span><span class="s2">&quot;</span><span class="se">\n</span><span class="s2">&quot;</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">out</span><span class="o">.</span><span class="n">write</span><span class="p">(</span><span class="s2">&quot; &quot;</span><span class="o">.</span><span class="n">join</span><span class="p">([(</span><span class="s2">&quot;</span><span class="si">%d</span><span class="s2">&quot;</span> <span class="o">%</span> <span class="n">Z</span><span class="p">)</span> <span class="k">for</span> <span class="n">Z</span> <span class="ow">in</span> <span class="n">atnums</span><span class="p">])</span><span class="o">+</span><span class="s2">&quot;</span><span class="se">\n</span><span class="s2">&quot;</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">out</span><span class="o">.</span><span class="n">write</span><span class="p">(</span><span class="s2">&quot;Selective Dynamics</span><span class="se">\n</span><span class="s2">&quot;</span><span class="p">)</span>
        <span class="k">if</span> <span class="p">(</span><span class="nb">hasattr</span><span class="p">(</span><span class="n">at</span><span class="p">,</span> <span class="s1">&#39;VASP_Pos_Format&#39;</span><span class="p">)):</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">out</span><span class="o">.</span><span class="n">write</span><span class="p">(</span><span class="n">at</span><span class="o">.</span><span class="n">params</span><span class="p">[</span><span class="s1">&#39;VASP_Pos_Format&#39;</span><span class="p">]</span><span class="o">+</span><span class="s2">&quot;</span><span class="se">\n</span><span class="s2">&quot;</span><span class="p">)</span>
            <span class="k">if</span> <span class="p">((</span><span class="n">at</span><span class="o">.</span><span class="n">params</span><span class="p">[</span><span class="s1">&#39;VASP_Pos_Format&#39;</span><span class="p">][</span><span class="mi">0</span><span class="p">]</span> <span class="o">==</span> <span class="s1">&#39;d&#39;</span> <span class="ow">or</span> <span class="n">at</span><span class="o">.</span><span class="n">params</span><span class="p">[</span><span class="s1">&#39;VASP_Pos_Format&#39;</span><span class="p">][</span><span class="mi">0</span><span class="p">]</span> <span class="o">==</span> <span class="s1">&#39;D&#39;</span><span class="p">)</span> <span class="ow">and</span> <span class="n">swapped_a1_a2</span><span class="p">):</span>
                <span class="n">t_p1</span> <span class="o">=</span> <span class="n">at</span><span class="o">.</span><span class="n">pos</span><span class="p">[</span><span class="mi">1</span><span class="p">,:]</span><span class="o">.</span><span class="n">copy</span><span class="p">()</span>
                <span class="n">at</span><span class="o">.</span><span class="n">pos</span><span class="p">[</span><span class="mi">1</span><span class="p">,:]</span> <span class="o">=</span> <span class="n">at</span><span class="o">.</span><span class="n">pos</span><span class="p">[</span><span class="mi">2</span><span class="p">,:]</span>
                <span class="n">at</span><span class="o">.</span><span class="n">pos</span><span class="p">[</span><span class="mi">2</span><span class="p">,:]</span> <span class="o">=</span> <span class="n">t_p1</span><span class="p">[:]</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">out</span><span class="o">.</span><span class="n">write</span><span class="p">(</span><span class="s2">&quot;Cartesian</span><span class="se">\n</span><span class="s2">&quot;</span><span class="p">)</span>

        <span class="c1"># Positions</span>
        <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="nb">len</span><span class="p">(</span><span class="n">prop_vals</span><span class="p">)):</span>
            <span class="k">for</span> <span class="n">i_at</span> <span class="ow">in</span> <span class="n">frange</span><span class="p">(</span><span class="n">at</span><span class="o">.</span><span class="n">n</span><span class="p">):</span>
                <span class="k">try</span><span class="p">:</span>
                    <span class="n">match</span> <span class="o">=</span> <span class="nb">getattr</span><span class="p">(</span><span class="n">at</span><span class="p">,</span><span class="nb">property</span><span class="p">)[</span><span class="n">i_at</span><span class="p">]</span><span class="o">.</span><span class="n">stripstrings</span><span class="p">()</span> <span class="o">==</span> <span class="n">prop_vals</span><span class="p">[</span><span class="n">i</span><span class="p">]</span>
                <span class="k">except</span><span class="p">:</span>
                    <span class="n">match</span> <span class="o">=</span> <span class="nb">getattr</span><span class="p">(</span><span class="n">at</span><span class="p">,</span><span class="nb">property</span><span class="p">)[</span><span class="n">i_at</span><span class="p">]</span> <span class="o">==</span> <span class="n">prop_vals</span><span class="p">[</span><span class="n">i</span><span class="p">]</span>
                <span class="k">if</span> <span class="p">(</span><span class="n">match</span><span class="p">):</span>
                    <span class="bp">self</span><span class="o">.</span><span class="n">out</span><span class="o">.</span><span class="n">write</span><span class="p">(</span><span class="s2">&quot;</span><span class="si">%.12f</span><span class="s2"> </span><span class="si">%.12f</span><span class="s2"> </span><span class="si">%.12f</span><span class="s2">   T T T</span><span class="se">\n</span><span class="s2">&quot;</span> <span class="o">%</span> <span class="p">(</span><span class="n">at</span><span class="o">.</span><span class="n">pos</span><span class="p">[</span><span class="mi">1</span><span class="p">,</span><span class="n">i_at</span><span class="p">],</span> <span class="n">at</span><span class="o">.</span><span class="n">pos</span><span class="p">[</span><span class="mi">2</span><span class="p">,</span><span class="n">i_at</span><span class="p">],</span> <span class="n">at</span><span class="o">.</span><span class="n">pos</span><span class="p">[</span><span class="mi">3</span><span class="p">,</span><span class="n">i_at</span><span class="p">]))</span>

        <span class="c1"># Velocities</span>
        <span class="k">if</span> <span class="p">(</span><span class="nb">hasattr</span><span class="p">(</span><span class="n">at</span><span class="p">,</span> <span class="s1">&#39;velo&#39;</span><span class="p">)):</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">out</span><span class="o">.</span><span class="n">write</span><span class="p">(</span><span class="s2">&quot;</span><span class="se">\n</span><span class="s2">&quot;</span><span class="p">)</span>
            <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="nb">len</span><span class="p">(</span><span class="n">prop_vals</span><span class="p">)):</span>
                <span class="k">for</span> <span class="n">i_at</span> <span class="ow">in</span> <span class="n">frange</span><span class="p">(</span><span class="n">at</span><span class="o">.</span><span class="n">n</span><span class="p">):</span>
                    <span class="k">try</span><span class="p">:</span>
                        <span class="n">match</span> <span class="o">=</span> <span class="nb">getattr</span><span class="p">(</span><span class="n">at</span><span class="p">,</span><span class="nb">property</span><span class="p">)[</span><span class="n">i_at</span><span class="p">]</span><span class="o">.</span><span class="n">stripstrings</span><span class="p">()</span> <span class="o">==</span> <span class="n">prop_vals</span><span class="p">[</span><span class="n">i</span><span class="p">]</span>
                    <span class="k">except</span><span class="p">:</span>
                        <span class="n">match</span> <span class="o">=</span> <span class="nb">getattr</span><span class="p">(</span><span class="n">at</span><span class="p">,</span><span class="nb">property</span><span class="p">)[</span><span class="n">i_at</span><span class="p">]</span> <span class="o">==</span> <span class="n">prop_vals</span><span class="p">[</span><span class="n">i</span><span class="p">]</span>
                    <span class="k">if</span> <span class="p">(</span><span class="n">match</span><span class="p">):</span>
                        <span class="bp">self</span><span class="o">.</span><span class="n">out</span><span class="o">.</span><span class="n">write</span><span class="p">(</span><span class="s2">&quot;</span><span class="si">%f</span><span class="s2"> </span><span class="si">%f</span><span class="s2"> </span><span class="si">%f</span><span class="se">\n</span><span class="s2">&quot;</span> <span class="o">%</span> <span class="p">(</span><span class="n">at</span><span class="o">.</span><span class="n">velo</span><span class="p">[</span><span class="mi">1</span><span class="p">,</span><span class="n">i_at</span><span class="p">],</span> <span class="n">at</span><span class="o">.</span><span class="n">velo</span><span class="p">[</span><span class="mi">2</span><span class="p">,</span><span class="n">i_at</span><span class="p">],</span> <span class="n">at</span><span class="o">.</span><span class="n">velo</span><span class="p">[</span><span class="mi">3</span><span class="p">,</span><span class="n">i_at</span><span class="p">]))</span></div>

<span class="n">AtomsWriters</span><span class="p">[</span><span class="s1">&#39;vasp&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="n">VASPWriter</span>
<span class="n">AtomsWriters</span><span class="p">[</span><span class="s1">&#39;VASP&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="n">VASPWriter</span>
<span class="n">AtomsWriters</span><span class="p">[</span><span class="s1">&#39;POSCAR&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="n">VASPWriter</span>

<div class="viewcode-block" id="VASP_POSCAR_Reader"><a class="viewcode-back" href="../../io.html#quippy.vasp.VASP_POSCAR_Reader">[docs]</a><span class="nd">@atoms_reader</span><span class="p">(</span><span class="s1">&#39;OUTCAR&#39;</span><span class="p">)</span>
<span class="k">def</span> <span class="nf">VASP_POSCAR_Reader</span><span class="p">(</span><span class="n">outcar</span><span class="p">,</span> <span class="n">species</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="nb">format</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;Read a configuration from a VASP OUTCAR file.&quot;&quot;&quot;</span>

    <span class="k">if</span> <span class="p">(</span><span class="n">outcar</span> <span class="o">==</span> <span class="s1">&#39;stdin&#39;</span> <span class="ow">or</span> <span class="n">outcar</span> <span class="o">==</span> <span class="s1">&#39;-&#39;</span><span class="p">):</span>
        <span class="n">p</span> <span class="o">=</span> <span class="n">sys</span><span class="o">.</span><span class="n">stdin</span>
    <span class="k">else</span><span class="p">:</span>
        <span class="n">p</span> <span class="o">=</span> <span class="nb">open</span><span class="p">(</span><span class="n">outcar</span><span class="p">,</span> <span class="s1">&#39;r&#39;</span><span class="p">)</span>

    <span class="n">re_comment</span> <span class="o">=</span> <span class="n">re</span><span class="o">.</span><span class="n">compile</span><span class="p">(</span><span class="s2">&quot;\s*POSCAR:\s*(.+)&quot;</span><span class="p">)</span>
    <span class="n">re_potcar</span> <span class="o">=</span> <span class="n">re</span><span class="o">.</span><span class="n">compile</span><span class="p">(</span><span class="s2">&quot;\s*POTCAR:\s*\S+\s+(\S+)&quot;</span><span class="p">)</span>
    <span class="n">re_n_atoms</span> <span class="o">=</span> <span class="n">re</span><span class="o">.</span><span class="n">compile</span><span class="p">(</span><span class="s2">&quot;\s*ions per type =\s*((?:\d+\s*)*)&quot;</span><span class="p">)</span>

    <span class="n">energy_i</span> <span class="o">=</span> <span class="o">-</span><span class="mi">1</span>
    <span class="n">at_i</span> <span class="o">=</span> <span class="o">-</span><span class="mi">1</span>
    <span class="n">lat_i</span> <span class="o">=</span> <span class="o">-</span><span class="mi">1</span>
    <span class="n">elements</span><span class="o">=</span><span class="p">[]</span>
    <span class="n">n_at</span> <span class="o">=</span> <span class="o">-</span><span class="mi">1</span>
    <span class="n">at_cur</span> <span class="o">=</span> <span class="kc">None</span>
    <span class="k">for</span> <span class="n">lr</span> <span class="ow">in</span> <span class="n">p</span><span class="p">:</span>
        <span class="n">l</span><span class="o">=</span><span class="n">lr</span><span class="o">.</span><span class="n">rstrip</span><span class="p">()</span>
        <span class="k">if</span> <span class="p">(</span><span class="n">n_at</span> <span class="o">&lt;=</span> <span class="mi">0</span><span class="p">):</span>
            <span class="c1"># parse header type things</span>
            <span class="n">m</span> <span class="o">=</span> <span class="n">re_comment</span><span class="o">.</span><span class="n">match</span><span class="p">(</span><span class="n">l</span><span class="p">)</span>
            <span class="k">if</span> <span class="p">(</span><span class="n">m</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">):</span>
                <span class="n">VASP_Comment</span><span class="o">=</span><span class="n">m</span><span class="o">.</span><span class="n">group</span><span class="p">(</span><span class="mi">1</span><span class="p">)</span>
                <span class="c1"># print &quot;got VASP_Comment &#39;%s&#39;&quot; % VASP_Comment</span>
            <span class="n">m</span> <span class="o">=</span> <span class="n">re_potcar</span><span class="o">.</span><span class="n">match</span><span class="p">(</span><span class="n">l</span><span class="p">)</span>
            <span class="k">if</span> <span class="p">(</span><span class="n">m</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">):</span>
                <span class="n">elements</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">m</span><span class="o">.</span><span class="n">group</span><span class="p">(</span><span class="mi">1</span><span class="p">))</span>
            <span class="n">m</span> <span class="o">=</span> <span class="n">re_n_atoms</span><span class="o">.</span><span class="n">match</span><span class="p">(</span><span class="n">l</span><span class="p">)</span>
            <span class="k">if</span> <span class="p">(</span><span class="n">m</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">):</span>
                <span class="c1"># print &quot;got ions per type, groups are:&quot;</span>
                <span class="c1"># print m.groups()</span>
                <span class="n">lat</span> <span class="o">=</span> <span class="n">fzeros</span><span class="p">(</span> <span class="p">(</span><span class="mi">3</span><span class="p">,</span><span class="mi">3</span><span class="p">)</span> <span class="p">)</span>
                <span class="n">n_types</span> <span class="o">=</span> <span class="p">[</span> <span class="nb">int</span><span class="p">(</span><span class="n">f</span><span class="p">)</span> <span class="k">for</span> <span class="n">f</span> <span class="ow">in</span> <span class="n">m</span><span class="o">.</span><span class="n">group</span><span class="p">(</span><span class="mi">1</span><span class="p">)</span><span class="o">.</span><span class="n">split</span><span class="p">()</span> <span class="p">]</span>
                <span class="n">n_at</span><span class="o">=</span><span class="nb">sum</span><span class="p">(</span><span class="n">n_types</span><span class="p">)</span>
                <span class="n">at</span> <span class="o">=</span> <span class="n">Atoms</span><span class="p">(</span><span class="n">n</span><span class="o">=</span><span class="n">n_at</span><span class="p">,</span> <span class="n">latttice</span><span class="o">=</span><span class="n">lat</span><span class="p">)</span>
                <span class="n">i_at</span> <span class="o">=</span> <span class="mi">0</span>
                <span class="k">for</span> <span class="n">type_i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="nb">len</span><span class="p">(</span><span class="n">n_types</span><span class="p">)):</span>
                    <span class="k">for</span> <span class="n">j</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="n">n_types</span><span class="p">[</span><span class="n">type_i</span><span class="p">]):</span>
                        <span class="n">i_at</span> <span class="o">+=</span> <span class="mi">1</span>
                        <span class="c1"># print &quot;set species of atom %d to &#39;%s&#39;&quot; % (i_at, elements[type_i])</span>
                        <span class="n">at</span><span class="o">.</span><span class="n">species</span><span class="p">[</span><span class="n">i_at</span><span class="p">]</span> <span class="o">=</span> <span class="n">elements</span><span class="p">[</span><span class="n">type_i</span><span class="p">]</span>
                <span class="n">at</span><span class="o">.</span><span class="n">set_zs</span><span class="p">()</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="c1"># parse per-config lattice/pos/force</span>
            <span class="k">if</span> <span class="p">(</span><span class="n">l</span><span class="o">.</span><span class="n">find</span><span class="p">(</span><span class="s2">&quot;direct lattice vectors&quot;</span><span class="p">)</span> <span class="o">&gt;=</span> <span class="mi">0</span><span class="p">):</span> <span class="c1"># get ready to read lattice vectors</span>
                <span class="n">at_cur</span> <span class="o">=</span> <span class="n">at</span><span class="o">.</span><span class="n">copy</span><span class="p">()</span>
                <span class="n">lat_cur</span><span class="o">=</span><span class="n">fzeros</span><span class="p">(</span> <span class="p">(</span><span class="mi">3</span><span class="p">,</span><span class="mi">3</span><span class="p">)</span> <span class="p">)</span>
                <span class="n">lat_i</span><span class="o">=</span><span class="mi">1</span>
            <span class="k">elif</span> <span class="p">(</span><span class="n">lat_i</span> <span class="o">&gt;=</span> <span class="mi">1</span> <span class="ow">and</span> <span class="n">lat_i</span> <span class="o">&lt;=</span> <span class="mi">3</span><span class="p">):</span> <span class="c1"># read lattice vectors</span>
                <span class="n">lat_cur</span><span class="p">[:,</span><span class="n">lat_i</span><span class="p">]</span> <span class="o">=</span> <span class="p">[</span> <span class="nb">float</span><span class="p">(</span><span class="n">r</span><span class="p">)</span> <span class="k">for</span> <span class="n">r</span> <span class="ow">in</span> <span class="n">l</span><span class="o">.</span><span class="n">replace</span><span class="p">(</span><span class="s2">&quot;-&quot;</span><span class="p">,</span><span class="s2">&quot; -&quot;</span><span class="p">)</span><span class="o">.</span><span class="n">split</span><span class="p">()[</span><span class="mi">0</span><span class="p">:</span><span class="mi">3</span><span class="p">]</span> <span class="p">]</span>
                <span class="n">lat_i</span> <span class="o">+=</span> <span class="mi">1</span>
            <span class="k">elif</span> <span class="p">(</span><span class="n">l</span><span class="o">.</span><span class="n">find</span><span class="p">(</span><span class="s2">&quot;TOTAL-FORCE (eV/Angst)&quot;</span><span class="p">)</span> <span class="o">&gt;=</span> <span class="mi">0</span><span class="p">):</span> <span class="c1"># get ready to read atomic positions and forces</span>
                <span class="k">if</span> <span class="p">(</span><span class="ow">not</span> <span class="nb">hasattr</span><span class="p">(</span><span class="n">at_cur</span><span class="p">,</span> <span class="s2">&quot;force&quot;</span><span class="p">)):</span>
                    <span class="n">at_cur</span><span class="o">.</span><span class="n">add_property</span><span class="p">(</span><span class="s2">&quot;force&quot;</span><span class="p">,</span> <span class="mf">0.0</span><span class="p">,</span> <span class="n">n_cols</span><span class="o">=</span><span class="mi">3</span><span class="p">)</span>
                <span class="n">at_i</span><span class="o">=</span><span class="mi">1</span>
                <span class="n">p</span><span class="o">.</span><span class="n">next</span><span class="p">()</span>
            <span class="k">elif</span> <span class="p">(</span><span class="n">at_i</span> <span class="o">&gt;=</span> <span class="mi">1</span> <span class="ow">and</span> <span class="n">at_i</span> <span class="o">&lt;=</span> <span class="n">at_cur</span><span class="o">.</span><span class="n">n</span><span class="p">):</span> <span class="c1"># read atomic positions and forces</span>
                <span class="n">pos_force</span> <span class="o">=</span> <span class="p">[</span> <span class="nb">float</span><span class="p">(</span><span class="n">r</span><span class="p">)</span> <span class="k">for</span> <span class="n">r</span> <span class="ow">in</span> <span class="n">l</span><span class="o">.</span><span class="n">replace</span><span class="p">(</span><span class="s2">&quot;-&quot;</span><span class="p">,</span><span class="s2">&quot; -&quot;</span><span class="p">)</span><span class="o">.</span><span class="n">split</span><span class="p">()[</span><span class="mi">0</span><span class="p">:</span><span class="mi">6</span><span class="p">]</span> <span class="p">]</span>
                <span class="n">at_cur</span><span class="o">.</span><span class="n">pos</span><span class="p">[:,</span><span class="n">at_i</span><span class="p">]</span> <span class="o">=</span> <span class="n">pos_force</span><span class="p">[</span><span class="mi">0</span><span class="p">:</span><span class="mi">3</span><span class="p">]</span>
                <span class="n">at_cur</span><span class="o">.</span><span class="n">force</span><span class="p">[:,</span><span class="n">at_i</span><span class="p">]</span> <span class="o">=</span> <span class="n">pos_force</span><span class="p">[</span><span class="mi">3</span><span class="p">:</span><span class="mi">6</span><span class="p">]</span>
                <span class="n">at_i</span> <span class="o">+=</span> <span class="mi">1</span>
            <span class="k">elif</span> <span class="p">(</span><span class="n">l</span><span class="o">.</span><span class="n">find</span><span class="p">(</span><span class="s2">&quot;free  energy&quot;</span><span class="p">)</span> <span class="o">&gt;=</span> <span class="mi">0</span><span class="p">):</span> <span class="c1"># get ready to read energy</span>
                <span class="n">at_cur</span><span class="o">.</span><span class="n">params</span><span class="p">[</span><span class="s1">&#39;Energy&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="nb">float</span><span class="p">(</span><span class="n">l</span><span class="o">.</span><span class="n">split</span><span class="p">()[</span><span class="mi">4</span><span class="p">])</span>
                <span class="n">energy_i</span> <span class="o">=</span> <span class="mi">1</span>
                <span class="n">p</span><span class="o">.</span><span class="n">next</span><span class="p">()</span>
            <span class="k">elif</span> <span class="p">(</span><span class="n">energy_i</span> <span class="o">==</span> <span class="mi">1</span><span class="p">):</span> <span class="c1"># read energy</span>
                <span class="c1"># print &quot;energy(sigma-&gt;0) line&quot;</span>
                <span class="c1"># print l.split()</span>
                <span class="n">at_cur</span><span class="o">.</span><span class="n">params</span><span class="p">[</span><span class="s1">&#39;Energy_sigma_to_zero&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="nb">float</span><span class="p">(</span><span class="n">l</span><span class="o">.</span><span class="n">split</span><span class="p">()[</span><span class="mi">6</span><span class="p">])</span>
                <span class="n">energy_i</span> <span class="o">+=</span> <span class="mi">1</span>
                <span class="k">yield</span> <span class="n">at_cur</span>
            <span class="k">if</span> <span class="p">(</span><span class="n">at_cur</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span> <span class="ow">and</span> <span class="n">at_i</span> <span class="o">==</span> <span class="n">at_cur</span><span class="o">.</span><span class="n">n</span><span class="p">):</span> <span class="c1"># at end of configuration, set lattice</span>
                <span class="n">at_cur</span><span class="o">.</span><span class="n">set_lattice</span><span class="p">(</span><span class="n">lat_cur</span><span class="p">,</span> <span class="kc">False</span><span class="p">)</span>

    <span class="n">__all__</span> <span class="o">=</span> <span class="p">[</span><span class="s1">&#39;VASP_POSCAR_Reader&#39;</span><span class="p">,</span> <span class="s1">&#39;VASP_OUTCAR_Reader&#39;</span><span class="p">,</span> <span class="s1">&#39;VASPWriter&#39;</span><span class="p">]</span></div>

<span class="k">try</span><span class="p">:</span>
    <span class="c1"># use ASE vasp readers/writers if they are available</span>
    <span class="kn">import</span> <span class="nn">ase.io.vasp</span>
    <span class="kn">from</span> <span class="nn">quippy.io</span> <span class="k">import</span> <span class="n">ASEReader</span><span class="p">,</span> <span class="n">ASEWriter</span>

    <span class="c1"># make old readers accessible with an underscore prefix</span>
    <span class="n">AtomsReaders</span><span class="p">[</span><span class="s1">&#39;_POSCAR&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="n">AtomsReaders</span><span class="p">[</span><span class="s1">&#39;POSCAR&#39;</span><span class="p">]</span>
    <span class="n">AtomsReaders</span><span class="p">[</span><span class="s1">&#39;_OUTCAR&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="n">AtomsReaders</span><span class="p">[</span><span class="s1">&#39;OUTCAR&#39;</span><span class="p">]</span>

    <span class="n">AtomsReaders</span><span class="p">[</span><span class="s1">&#39;POSCAR&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="n">ASEReader</span>
    <span class="n">AtomsReaders</span><span class="p">[</span><span class="s1">&#39;CONTCAR&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="n">ASEReader</span>
    <span class="n">AtomsReaders</span><span class="p">[</span><span class="s1">&#39;vasp&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="n">ASEReader</span>
    <span class="n">AtomsReaders</span><span class="p">[</span><span class="s1">&#39;OUTCAR&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="n">ASEReader</span>
    <span class="n">AtomsReaders</span><span class="p">[</span><span class="s1">&#39;vasp-out&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="n">ASEReader</span>

    <span class="n">AtomsWriters</span><span class="p">[</span><span class="s1">&#39;POSCAR&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="n">ASEWriter</span>
    <span class="n">AtomsWriters</span><span class="p">[</span><span class="s1">&#39;CONTCAR&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="n">ASEWriter</span>
    <span class="n">AtomsWriters</span><span class="p">[</span><span class="s1">&#39;vasp&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="n">ASEWriter</span>

<span class="k">except</span> <span class="ne">ImportError</span><span class="p">:</span>
    <span class="k">pass</span>
</pre></div>

           </div>
           
          </div>
          <footer>
  

  <hr/>

  <div role="contentinfo">
    <p>
        &copy; Copyright 2008-2016, James Kermode

    </p>
  </div>
  Built with <a href="http://sphinx-doc.org/">Sphinx</a> using a <a href="https://github.com/rtfd/sphinx_rtd_theme">theme</a> provided by <a href="https://readthedocs.org">Read the Docs</a>. 

</footer>

        </div>
      </div>

    </section>

  </div>
  


  <script type="text/javascript">
      jQuery(function () {
          SphinxRtdTheme.Navigation.enable(true);
      });
  </script>

  
  
    
   

</body>
</html>